#define _CRT_SECURE_NO_WARNINGS 1
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
	vector<TreeNode*> generateTrees(int l, int r)
	{
		if (l > r) return { nullptr };
		vector<TreeNode*> allTrees;

		for (int i = l; i <= r; i++)
		{
			vector<TreeNode*> left = generateTrees(l, i - 1);
			vector<TreeNode*> right = generateTrees(i + 1, r);
			for (auto& lt : left)
			{
				for (auto& rt : right)
				{
					TreeNode* cur = new TreeNode(i);
					cur->left = lt;
					cur->right = rt;
					allTrees.push_back(cur);
				}
			}
		}
		return allTrees;
	}
	vector<TreeNode*> generateTrees(int n) {
		return generateTrees(1, n);
	}
};